//
//  WheelImageTool.m
//  IFengNews
//
//  Created by lanou3g on 16/3/3.
//  Copyright © 2016年 徐强. All rights reserved.
//

#import "WheelImageTool.h"
#import "UIImageView+WebCache.h"
@interface WheelImageTool()<UIScrollViewDelegate>
{
    NSUInteger count;//记录图片个数
    NSUInteger currentNum;//记录当前播放的张数
}
@property (nonatomic,strong)UIScrollView *mainScroller;
@property (nonatomic,strong)UIPageControl *page;
@property (nonatomic,strong)NSTimer *timer;
//轮播图片之间的间隔
@property (nonatomic,assign)CGFloat timeInterval;
@end

@implementation WheelImageTool

//  用这个方法初始化  1.frame     2.存放图片url字符串的数组   3.轮播时间间隔
- (instancetype )initWithFrame:(CGRect)frame imageUrlArr:(NSArray *)arr timeInterval:(CGFloat)timeInterval
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setViewWithImageUrlArr:arr];
        // 保存一下输入的时间间隔
        self.timeInterval = timeInterval;
        count = arr.count;
        currentNum = 1;
        if(timeInterval ){
            self.timer = [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
        }
    }
    return self;
}
//定时器的方法
- (void)timerAction{
    if (count == 1) {
        //只有一张图片的时候没有轮播效果
        [self.mainScroller setContentOffset:CGPointMake(0, 0) animated:NO];
        HeaderModel *model = self.array[0];
        self.label.text = model.title;
    }else{
        currentNum ++;
        if (currentNum == count+1)
        {
            //播放到最后一张时
            self.page.currentPage = 0;
            //无动画直接跳转到第一张，并且无时间用动画跳转到第二张（这样避免了往回图片回播的动画，达到一直往右边轮播的效果）
            [self.mainScroller setContentOffset:CGPointMake(0, 0) animated:NO];
            [self.mainScroller setContentOffset:CGPointMake(self.frame.size.width, 0) animated:YES];
            currentNum = 1;
            return; 
        }
        [self.mainScroller setContentOffset:CGPointMake(currentNum *self.frame.size.width, 0) animated:YES];
        //page 的点跟随偏移量改变
        self.page.currentPage = currentNum - 1;
    }
}
#pragma mark -scroll的代理方法 ==== 结束减速时的操作
//结束减速时的操作    用来滑动图片对应page的圆点
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //根据偏移量来计算显示的是第几张图片，从而改变page
    currentNum = self.mainScroller.contentOffset.x / self.frame.size.width;
    if (currentNum == count + 1) {
        //向右滑动图片到最后一张时，直接跳到第二张
        self.page.currentPage = 0 ;
        currentNum = 1;
        [self.mainScroller setContentOffset:CGPointMake(currentNum * self.frame.size.width, 0) animated:NO];
    }else if (currentNum == 0){
        //向左滑动图片到第一张时，直接跳到倒数第二张
        self.page.currentPage = count - 1;
        currentNum = count ;
        [self.mainScroller setContentOffset:CGPointMake(currentNum * self.frame.size.width, 0) animated:NO];
    }else{
        self.page.currentPage = currentNum - 1 ;
    }
}
//私有方法
- (void)setViewWithImageUrlArr:(NSArray *)arr{
    if (!arr) {
        return;
    }
    //设置mainScroller
    self.mainScroller = [[UIScrollView alloc]initWithFrame:self.frame];
    self.mainScroller.contentSize = CGSizeMake((arr.count+2)*self.frame.size.width, 0);
    if (count == 1) {
        //如果只有一张图片
        //不轮播也不能滑动
        self.mainScroller.scrollEnabled = NO;
    }
    self.mainScroller.delegate = self;
    //整页滑动
    self.mainScroller.pagingEnabled = YES;
    //关闭反弹效果
    self.mainScroller.bounces = NO;
    //不显示滚动条
    self.mainScroller.showsHorizontalScrollIndicator = NO;
    //初始偏移量为传入的第一张图（轮播图组的第二张）
    self.mainScroller.contentOffset = CGPointMake(self.frame.size.width, 0);
    [self addSubview:self.mainScroller];
    //将所有图片排好顺序（总共n+2张，第一张加在最后一张，最后一张加载第一张）
    //第一张
    UIImageView *imV1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    imV1.userInteractionEnabled = YES;
    [imV1 sd_setImageWithURL:arr.lastObject];
    [self.mainScroller addSubview:imV1];
    //按顺序加入数组中的图片
    for (int i = 0 ; i < arr.count ; i++) {
        UIImageView *imV = [[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width * (i+1), 0, self.frame.size.width, self.frame.size.height)];
        imV.userInteractionEnabled = YES;
        [imV sd_setImageWithURL:arr[i]];
        [self.mainScroller addSubview:imV];
    }
    //最后一张
    UIImageView *imV2 = [[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width * ( arr.count + 1 ), 0, self.frame.size.width, self.frame.size.height)];
    imV2.userInteractionEnabled = YES;
    [imV2 sd_setImageWithURL:arr.firstObject];
    [self.mainScroller addSubview:imV2];
    //设置一下pageControl:设置在右下角
    self.page = [[UIPageControl alloc]initWithFrame:CGRectMake(self.frame.size.width - 80, self.frame.size.height - 30, 80, 30)];
    self.page.enabled = NO;//不可点击
    self.page.numberOfPages = arr.count;
    [self addSubview:self.page];
}
@end
